package neusoft.edu.cn.guanshangshiguang.fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.LocationClient;

import neusoft.edu.cn.guanshangshiguang.Listener.Listener;
import neusoft.edu.cn.guanshangshiguang.Listener.MyLocationListener;
import neusoft.edu.cn.guanshangshiguang.Listener.LocationIface;
import neusoft.edu.cn.guanshangshiguang.R;
import neusoft.edu.cn.guanshangshiguang.activities.LoginActivity;
import neusoft.edu.cn.guanshangshiguang.bean.User_inforBean;
import neusoft.edu.cn.guanshangshiguang.model.LocationModel;
import neusoft.edu.cn.guanshangshiguang.model.UserInforModel;

public class UserFragment extends Fragment {

    TextView show,yonghuming,sex,zhuanye,xuexiao,xuehao;
    String addr;
    private User_inforBean user_inforBean;

    Listener listener = new Listener() {
        @Override
        public void onResponse(Object o) {
            user_inforBean = (User_inforBean)o;

            yonghuming.setText(user_inforBean.getUname());
            if(user_inforBean.getGender().toString().equals("1"))
                sex.setText("小哥哥喔");
            else sex.setText("小姐姐诶");
            xuehao.setText(user_inforBean.getStudent_id());
            xuexiao.setText(user_inforBean.getSchool());
            zhuanye.setText(user_inforBean.getDepartment());
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getContext(), "加载个人信息失败...", Toast.LENGTH_SHORT).show();
        }
    };

    public LocationClient mLocationClient = null;
    private MyLocationListener myListener = new MyLocationListener();
    private LocationIface locationIface = new LocationIface() {
        @Override
        public void getResultList(String address) {
            addr=address;
        }
    };

    public UserFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_user, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
        Location();

        UserInforModel userInforModel = new UserInforModel();
        userInforModel.getResultList(LoginActivity.UserId,listener);

    }

    private void Location() {
        //百度地图定位初始化
        mLocationClient = new LocationClient(getContext());//声明LocationClient类
        mLocationClient.registerLocationListener(myListener);//注册监听函数

        LocationModel locationModel = new LocationModel();
        locationModel.startLocate(mLocationClient);

        myListener.setListener(locationIface);

        show.setText(addr);
    }

    private void init(View view) {
        show = view.findViewById(R.id.show);
        yonghuming = view.findViewById(R.id.yonghuming);
        sex = view.findViewById(R.id.sex);
        xuexiao = view.findViewById(R.id.xuexiao);
        zhuanye = view.findViewById(R.id.zhuanye);
        xuehao = view.findViewById(R.id.xuehao);
    }

}
